#include <dbg.h>

#include <algorithm>

using namespace std;

// 贪心
class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int i = matrix.size() - 1;
        int j = 0;
        while (i >= 0 && j < matrix[0].size()) {
            if (matrix[i][j] > target) {
                --i;
            } else if (matrix[i][j] < target) {
                ++j;
            } else {
                return true;
            }
        }

        return false;
    }
};

// 二分
// class Solution {
// public:
//     bool searchMatrix(vector<vector<int>>& matrix, int target) {
//         for (const vector<int>& row : matrix) {
//             auto it = lower_bound(row.begin(), row.end(), target);
//             if (it != row.end() && *it == target) {
//                 return true;
//             }
//         }
//         return false;
//     }
// };

int main() {
    Solution s;

    vector<vector<int>> matrix{{1, 4, 7, 11, 15},
                               {2, 5, 8, 12, 19},
                               {3, 6, 9, 16, 22},
                               {10, 13, 14, 17, 24},
                               {18, 21, 23, 26, 30}};
    int target = 5;
    dbg(s.searchMatrix(matrix, target));

    return 0;
}